package common

import (
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/errcode"
	optionDto "git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/dto/option"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/clue/report"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/service/common"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type OptionController struct {
	controller.BaseController
	optionService common.OptionServiceIface
	reportService report.ReportServiceIface
}

// NewOptionController 备选项接口
func NewOptionController(optionService common.OptionServiceIface,
	reportService report.ReportServiceIface,
) *OptionController {
	return &OptionController{
		optionService: optionService,
		reportService: reportService,
	}
}

func (c *OptionController) RegisterRouter(g *gin.RouterGroup) {
	// 省市雷达商机类型筛选项接口
	g.GET("radar-oppty-type", c.radarOpptyType)
	// 省市雷达地区筛选项接口
	g.GET("radar-area", c.radarArea)
	g.GET("get-option-by-group", c.getOption)
	g.GET("get-invest-company", c.getInvestCompanyOption)
}

func (c *OptionController) setAnonymityUser(ctx *gin.Context) {
	// 如果是未付费用户
	tenantCode := authorize.GetTenantCode(ctx)
	if tenantCode == "" {
		urlUid := ctx.Request.Header.Get("url_uid")
		c.reportService.SetSharerCodeWithReportUrl(ctx, urlUid)
	}
}

func (c *OptionController) radarOpptyType(ctx *gin.Context) {
	result, err := c.optionService.RadarOpptyTypeOption(ctx, &optionDto.GetRadarOpptyTypeOptionRequest{})
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	c.Success(ctx, result)
}

func (c *OptionController) radarArea(ctx *gin.Context) {
	result, err := c.optionService.RadarAreaOption(ctx, &optionDto.GetRadarAreaOptionRequest{})
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	c.Success(ctx, result)
}

func (c *OptionController) getOption(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	var request optionDto.GetOptionRequest
	if err := ctx.ShouldBind(&request); err != nil {
		stark.Logger.Errorf(ctx, "http getRadarList 参数错误：%s", err.Error())
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数错误")
		return
	}
	result, err := c.optionService.GetOption(ctx, &request)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	c.Success(ctx, result)
}

func (c *OptionController) getInvestCompanyOption(ctx *gin.Context) {
	c.setAnonymityUser(ctx)
	var request optionDto.GetInvestCompanyRequest
	if err := ctx.ShouldBind(&request); err != nil {
		stark.Logger.Errorf(ctx, "http getRadarList 参数错误：%s", err.Error())
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "参数错误")
		return
	}
	result, err := c.optionService.GetInvestCompanyOption(ctx, &request)
	if err != nil {
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	c.Success(ctx, result.List)
}
